﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.View.Templates;
using VideoRentalShop.Controller;

namespace VideoRentalShop.View.Panel.ReturnStepPanel
{
    /// <summary>
    /// Logika interakcji dla klasy ChooseMoviesStepPanel.xaml
    /// </summary>
    public partial class ChooseMoviesStepPanel : StepPanelContent
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;


        public ChooseMoviesStepPanel()
        {
            InitializeComponent();
        }


        public override void ClearForm()
        {
            lstMovies.Items.Clear();
        }

        public override void RefreshForm()
        {
            lstMovies.Items.Clear();

            var item = _item as OrderReturn;
            if (item != null)
            {
                var movies = item.Movies;
                if (movies != null)
                {
                    foreach (var movie in movies)
                    {
                        var lstItem = new
                        {
                            Id = movie.MovieId,
                            Title = movie.Title,
                            Date = movie.Date,
                            Overheld = movie.OverHeld ? "Tak" : "Nie",
                            OverheldBool = movie.OverHeld
                        };

                        lstMovies.Items.Add(lstItem);

                        if (movie.Returned)
                        {
                            lstMovies.SelectedItems.Add(lstItem);
                        }
                    }
                }
            }
        }

        public override bool ValidateForm()
        {
            if (lstMovies.Items.Count == 0)
            {
                MessageBox.Show("Wybrany klient nie posiada żadnych filmów do oddania.", "Brak filmów");
                return false;
            }

            if (lstMovies.SelectedItems.Count == 0)
            {
                MessageBox.Show("Wymagane jest wybranie co najmniej jednego filmu.", "Uzupełnij dane");
                return false;
            }

            foreach (dynamic lstItem in lstMovies.Items)
            {
                if (lstItem.OverheldBool && !lstMovies.SelectedItems.Contains(lstItem))
                {
                    var result = MessageBox.Show(string.Format("Film '{0}' jest przetrzymywany przez klienta. Kontynuować mimo to?", lstItem.Title), "Ostrzeżenie", MessageBoxButton.YesNo, MessageBoxImage.Warning);

                    if (result != MessageBoxResult.Yes)
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        public override void UpdateItem()
        {
            var item = _item as OrderReturn;

            if (_item != null)
            {
                foreach (dynamic lstItem in lstMovies.Items)
                {
                    var isSelected = lstMovies.SelectedItems.Contains(lstItem);
                    var id = (long)lstItem.Id;

                    var movie = item.Movies.Where(x => x.MovieId == id).FirstOrDefault();
                    if (movie != null)
                    {
                        movie.Returned = isSelected;
                    }
                }
            }
        }

        private void lstMovies_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstSelected.Items.Clear();

            foreach (var item in lstMovies.SelectedItems)
            {
                lstSelected.Items.Add(item);
            }
        }
    }
}
